package cn.easybuy.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.easybuy.dao.BaseDao;
import cn.easybuy.dao.User_AddressDao;
import cn.easybuy.entity.User_Address;
import cn.easybuy.utils.DataSourceUtil;

public class User_AddressDaoImpl extends BaseDao implements User_AddressDao {

	ResultSet rs=null;
	
	public User_AddressDaoImpl(Connection conn) {
		super(conn);
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<User_Address> userAddress(String id) {

		String sql="select * from easybuy_user_address where userId=?";
		Object[]objArr = {id};
		//调用方法获得结果集
		rs=super.executeQuery(sql,objArr);
		List<User_Address> list=new ArrayList<User_Address>();
		try {
			while(rs.next()){
				User_Address ua=new User_Address();
				ua.setAddress(rs.getString("address"));
				ua.setCreateTime(rs.getString("createTime"));
				ua.setId(rs.getInt("createTime"));
				ua.setIsDefault(rs.getInt("isDefault"));
				ua.setRemark(rs.getString("remark"));
				ua.setUserId(rs.getInt("userId"));
				list.add(ua);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DataSourceUtil.closeAll(null, null, rs);
		}
		return list;
	}

	@Override
	public int addAddress(String id, String address, String remark) {
		String sql="INSERT INTO easybuy_user_address(userId,address,createTime,isDefault,remark) VALUES(?,?,NOW(),0,?)";
		Object[]objArr = {id,address,remark};
		return super.executeUpdate(sql, objArr);
	}

}
